package com.zl.customer.web;

import com.zl.customer.pojo.Customer;
import com.zl.customer.pojo.CustomerCondition;
import com.zl.customer.pojo.Page;
import com.zl.customer.service.CustomerService;
import com.zl.customer.service.impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 高级查询带分页
 * @author suke
 * @create 2021-07-20 10:24
 */
@WebServlet(name = "QueryCustomerByConditionServlet",urlPatterns = "/QueryCustomerByConditionServlet.action")
public class QueryCustomerByConditionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/HTML;charset=UTF-8");
        //2.获取请求参数
        String cname = request.getParameter("cname");
        String gender = request.getParameter("gender");
        String cellphone = request.getParameter("cellphone");
        String email = request.getParameter("email");


        //获取当前页码
        String pageIndexStr = request.getParameter("pageIndex");
        int pageIndex = 1;
        if(pageIndexStr != null && !pageIndexStr.isEmpty() ){
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        //获取页容量
        int pageSize = 3;
        String pageSizeStr = request.getParameter("pageSize");
        if(pageSizeStr != null && !pageSizeStr.isEmpty() ){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        //3.封装成条件类对象
        CustomerCondition  condition = new CustomerCondition();
        Customer customer = new Customer();
        customer.setCname("%"+cname+"%");
        customer.setSex(gender);
        customer.setCellphone("%"+cellphone+"%");
        customer.setEmail(email);
        condition.setCustomer(customer);
        condition.setPageIndex(pageIndex);
        condition.setPageSiez(pageSize);

        CustomerService  customerService = new CustomerServiceImpl();
        Page<Customer> pages  =  customerService.queryCondition(condition);
        //通过域,把数据传递给jsp
        request.setAttribute("pages",pages);
        //把查询条件保存到request域
        request.setAttribute("condition",condition);

        //转发到list2.jsp
        request.getRequestDispatcher("/jsp/list2.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
